package com.future.mayi.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Looper;
import android.os.Message;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;

public class LoginApiUtils implements Callback, PlatformActionListener {
    private static final int MSG_AUTH_CANCEL = 1;
    private static final int MSG_AUTH_ERROR = 2;
    private static final int MSG_AUTH_COMPLETE = 3;

    private OnLoginListener loginListener;
    private String platform;
    private Context context;
    private Handler handler;



    //接口
    public interface OnLoginListener {
        boolean onLogin(String platform, int state);
    }


    public LoginApiUtils() {
        handler = new Handler(Looper.getMainLooper(), this);
    }


    public void setPlatform(String platform) {

        this.platform = platform;
    }

    public void setOnLoginListener(OnLoginListener login) {
        this.loginListener = login;
    }

    public void login(Context context) {
        this.context = context.getApplicationContext();
        if (platform == null) {
            return;
        }

        Platform plat = ShareSDK.getPlatform(platform);
        if (plat == null) {
            return;
        }

        if (plat.isAuthValid()) {
            plat.removeAccount(true);
        }

//        plat.SSOSetting(false);
        plat.setPlatformActionListener(this);
        plat.showUser(null);
    }


    /**
     * 处理操作结果
     */
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_AUTH_CANCEL: {
                // 取消

                loginListener.onLogin(null, 2);
            }
            break;
            case MSG_AUTH_ERROR: {
                // 失败
                Throwable t = (Throwable) msg.obj;
                //String text = "caught error: " + t.getMessage();
                loginListener.onLogin(null, 3);
                t.printStackTrace();
            }
            break;
            case MSG_AUTH_COMPLETE: {
                // 成功
                Object[] objs = (Object[]) msg.obj;
                String plat = (String) objs[0];
                @SuppressWarnings("unchecked")
                HashMap<String, Object> res = (HashMap<String, Object>) objs[1];
                loginListener.onLogin(plat, 1);
            }
            break;
        }
        return false;
    }


    @Override
    public void onComplete(Platform plat, int action, HashMap<String, Object> res) {
        if (action == Platform.ACTION_USER_INFOR) {
            Message msg = new Message();
            msg.what = MSG_AUTH_COMPLETE;
            msg.arg2 = action;
            msg.obj = new Object[]{plat.getName(), res};
            handler.sendMessage(msg);


        }

    }

    @Override
    public void onError(Platform platform, int action, Throwable throwable) {
        if (action == Platform.ACTION_USER_INFOR) {
            Message msg = new Message();
            msg.what = MSG_AUTH_ERROR;
            msg.arg2 = action;
            msg.obj = throwable;
            handler.sendMessage(msg);
        }
        throwable.printStackTrace();
    }

    @Override
    public void onCancel(Platform platform, int action) {
        if (action == Platform.ACTION_USER_INFOR) {
            Message msg = new Message();
            msg.what = MSG_AUTH_CANCEL;
            msg.arg2 = action;
            msg.obj = platform;
            handler.sendMessage(msg);
        }
    }
}
